package com.ttloner.dao.impl;

import com.ttloner.pojo.User;
import com.ttloner.utils.JDBCUtils;

import java.sql.Connection;


/**
 * @author TTLoner
 * @date 2021/7/5 21:28
 * @description
 */
public class UserDAOImpl extends BaseDAO implements UserDAO{
    @Override
    public User queryUserByUsername(String username) {
        Connection conn = JDBCUtils.getConnection();
        String sql="select * from user where username=?";
        User user = queryForOne(conn, User.class, sql, username);
        JDBCUtils.close(conn);
        return user;
    }

    @Override
    public int saveUser(User user) {
        Connection conn = JDBCUtils.getConnection();
        String sql="insert into user(`id`,`username`,`password`,`email`) values(?,?,?,?)";
        int updateNum = update(conn, sql, user.getId(), user.getUsername(), user.getPassword(), user.getEmail());
        JDBCUtils.close(conn);
        return updateNum;
    }

    @Override
    public User queryUserByUsernameAndPassword(String username, String passworld) {
        Connection conn = JDBCUtils.getConnection();
        String sql="select * from user where username=? and password=?";
        User user = queryForOne(conn, User.class, sql, username, passworld);
        JDBCUtils.close(conn);
        return user;
    }

}
